package com.apchernykh.types;

import java.util.LinkedList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.Parcel;
import android.os.Parcelable;



public class StreamURLs implements Parcelable {
	List<String> mUrls = new LinkedList<String>();
	public List<Integer> mBRs = new LinkedList<Integer>();

	
	public StreamURLs(Parcel iParcel) {
		mUrls = iParcel.createStringArrayList();
		//mBRs = in.createIntArrayList();
	}
	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel oParcel, int iUnusedFlags) {
		oParcel.writeStringList(mUrls);
		//TODO: write mBRs
	}
	
	public boolean hasUrl(String iUrl)
	{
		for(String url : mUrls)
		{
			if(url.equalsIgnoreCase(iUrl))
			{
				return true;
			}
		}
		return false;
	}
	
	public static final Parcelable.Creator<StreamURLs> CREATOR = new Parcelable.Creator<StreamURLs>() {
		public StreamURLs createFromParcel(Parcel iParcel) {
			return new StreamURLs(iParcel);
		}

		public StreamURLs[] newArray(int iSize) {
			return new StreamURLs[iSize];
		}
	};

	
	public StreamURLs(String iLowBitrateUrl, String iHighBitrateUrl) {
		mUrls.add(iLowBitrateUrl);
		mBRs.add(32);
		mUrls.add(iHighBitrateUrl);
		mBRs.add(128);
	}

	public StreamURLs() {
		// TODO Auto-generated constructor stub
	}


	public void refresh(Node aNode) {
		NodeList allAudioStreams = ((Element) aNode)
				.getElementsByTagName("item");
		boolean aMp3Only = false;
		boolean aAacDetected = false;
		for (int iStreamIter = 0; iStreamIter < allAudioStreams.getLength(); ++iStreamIter) {
			Element aCurrentStreamElement = (Element) allAudioStreams.item(iStreamIter);
			if (aCurrentStreamElement.getAttribute("type").equalsIgnoreCase("mp3")) {
				mBRs.add(Integer.decode(aCurrentStreamElement.getAttribute("bitrate")));
				final String aRecognizedUri = aCurrentStreamElement.getAttribute("url");
				mUrls.add(aRecognizedUri);
				aMp3Only = true;
			}
			if (!aMp3Only
					&& aCurrentStreamElement.getAttribute("type").equalsIgnoreCase(
							"aac")) {
				mBRs.add(Integer.decode(aCurrentStreamElement.getAttribute("bitrate")));
				final String aRecognizedUri = aCurrentStreamElement.getAttribute("url");
				mUrls.add(aRecognizedUri);
				aAacDetected = true;
			}
		}

		// If we have read both mp3 and aac from config file, filter out aac.
		// TODO: rewrite
		if (aMp3Only && aAacDetected) {
			LinkedList<String> aNewUrls = new LinkedList<String>();
			LinkedList<Integer> aNewBRs = new LinkedList<Integer>();

			int i = 0;
			for (String u : mUrls) {
				if (!"aac".equals(u.substring(u.length() - 3))) {
					aNewUrls.add(u);
					aNewBRs.add(mBRs.get(i));
				}
				i++;
			}
			mBRs = aNewBRs;
			mUrls = aNewUrls;
		}
	}

	public String getURL(int iDesiredBitrate) {
		int i = 0;
		for (Integer br : mBRs) {
			if (br == iDesiredBitrate)
				return mUrls.get(i);
			i++;
		}
		return mUrls.get(0);
	}

	public String getURL(String iDesiredBitrate) {
		if(iDesiredBitrate.equals("low"))
			return getURL(32);
		if(iDesiredBitrate.equals("high"))
			return getURL(128);
		//Deprecated
//		for (String url : urls) {
//			if (url.indexOf(mDesiredBitrate) != -1)
//				return url;
//		}
		return mUrls.get(0);
	}

	public void copy(StreamURLs iMp3url) {
		mUrls.addAll(iMp3url.mUrls);
		mBRs.addAll(iMp3url.mBRs);
	}
}
